package top.lidee.module.clientApi.twice.applet;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.lidee.common.dto.UserInfo;
import top.lidee.common.service.MerchantService;
import top.lidee.common.util.TokenUtil;
import top.lidee.framework.enums.CommonExceptionEnum;
import top.lidee.framework.result.ResponseResult;
import top.lidee.repository.model.twice.vo.applet.GetByMerchantsIdEchoVo;
import top.lidee.repository.model.twice.vo.applet.MtMerchantVo;
import top.lidee.utils.StringUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author 张一恒
 * @version 1.0
 * @description: 商户需要拦截接口
 * @date 2024/9/27 10:07
 */
@Api(tags="小程序端-商户需要登录相关接口")
@RestController
@AllArgsConstructor
@RequestMapping(value = "/clientApi/applet/merchant/inter")
public class MtMerchantInterceptController {

    private final MerchantService merchantService;

    /**
     * 查询当前用户下的商户以及商户对应店铺和店铺商品信息
     */
    @RequestMapping(value = "/getCurrentUserCorrespondsMerchantsAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询当前用户下的商户以及商户对应店铺和店铺商品信息")
    public ResponseResult getCurrentUserCorrespondsMerchantsAll(HttpServletRequest request) {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        UserInfo userInfo = TokenUtil.getUserInfoByToken(token);
        if (userInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        ResponseResult responseResult = merchantService.getCurrentUserCorrespondsMerchantsAll(userInfo);
        return responseResult;
    }

    /**
     * 查询当前用户是否是商户
     */
    @RequestMapping(value = "/isMerchant", method = RequestMethod.GET)
    @ApiOperation(value = "查询当前用户是否是商户")
    @CrossOrigin
    public ResponseResult isMerchant(HttpServletRequest request) {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        UserInfo userInfo = TokenUtil.getUserInfoByToken(token);
        if (userInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        ResponseResult responseResult = merchantService.isMerchant(userInfo);

        return responseResult;
    }

    /**
     * 成为商户
     */
    @RequestMapping(value = "/becomeAMerchant", method = RequestMethod.POST)
    @ApiOperation(value = "成为商户")
    @CrossOrigin
    public ResponseResult becomeAMerchant(HttpServletRequest request, @RequestBody @Validated MtMerchantVo vo) {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        UserInfo userInfo = TokenUtil.getUserInfoByToken(token);
        if (userInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        ResponseResult responseResult = merchantService.becomeAMerchant(vo,userInfo);

        return responseResult;
    }


    /**
     * 根据当前登录用户下的merchantsId回显数据
     */
    @RequestMapping(value = "/getByMerchantsIdEcho", method = RequestMethod.GET)
    @ApiOperation(value = "根据当前登录用户下的merchantsId回显数据")
    public ResponseResult getByMerchantsIdEcho(HttpServletRequest request) {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        UserInfo userInfo = TokenUtil.getUserInfoByToken(token);
        if (userInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        ResponseResult responseResult = merchantService.getByMerchantsIdEcho(userInfo);
        return responseResult;
    }

    /**
     * 修改商户
     */
    @RequestMapping(value = "/updateByMerchants", method = RequestMethod.PUT)
    @ApiOperation(value = "修改商户")
    public ResponseResult updateByMerchants(HttpServletRequest request, @RequestBody @Validated GetByMerchantsIdEchoVo vo) {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        UserInfo userInfo = TokenUtil.getUserInfoByToken(token);
        if (userInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        ResponseResult responseResult = merchantService.updateByMerchants(userInfo,vo);
        return responseResult;
    }

}
